package in.muton.edushare.url;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.model.IModel;

import com.edushare.common.constants.URLConstants;
import com.edushare.common.exceptions.EduShareRuntimeException;
import com.edushare.common.util.StringUtil;
import com.edushare.common.util.URLUtils;
import com.edushare.common.vo.Assignments;
import com.edushare.common.vo.Batches;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Curricula;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Groups;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;
import com.edushare.common.vo.interfaces.IFileUploadable;

public class EntityPageParams {
	public static PageParameters getParams1(IModel<?> model, String additional) {
		PageParameters params = new PageParameters();
		Integer nextParamId = -1;

		if (model.getObject() instanceof Universities) {
			params.add((++nextParamId).toString(), ((Universities) model
					.getObject()).getUniqueName());
		} else if (model.getObject() instanceof Departments) {
			Departments dept = (Departments) model.getObject();
			params.add((++nextParamId).toString(), URLUtils.escapeSplChars(dept
					.getDeptName()));
			params.add((++nextParamId).toString(), dept.getDepartmentId()
					.toString());
		} else if (model.getObject() instanceof Courses) {
			Courses course = (Courses) model.getObject();
			params.add((++nextParamId).toString(), URLUtils
					.escapeSplChars(course.getCourseName()));
			params.add((++nextParamId).toString(), course.getCourseId()
					.toString());
		} else if (model.getObject() instanceof CourseInstances) {
			CourseInstances cInst = (CourseInstances) model.getObject();
			params.add((++nextParamId).toString(), URLUtils
					.escapeSplChars(cInst.getCourse().getCourseName()));
			params.add((++nextParamId).toString(), cInst.getCourse().getCourseId()
					.toString());
			params.add((++nextParamId).toString(), cInst.getCourseInstanceId()
					.toString());
		} else if (model.getObject() instanceof Users) {
			Users user = (Users) model.getObject();
			params.add((++nextParamId).toString(), URLUtils.escapeSplChars(user
					.getUserName()));
		} else if (model.getObject() instanceof Assignments) {
			Assignments assn = (Assignments) model.getObject();
			params.add((++nextParamId).toString(), assn.getAssignmentId()
					.toString());
		} else if (model.getObject() instanceof Groups) {
			Groups g = (Groups)model.getObject();
			params.add((++nextParamId).toString(), g.getGroupId()
					.toString());
		} else if(model.getObject() instanceof Curricula){
			Curricula curr = (Curricula)model.getObject();
			params.add((++nextParamId).toString(), curr.getCurriculaId().toString());
		} else if(model.getObject() instanceof Batches){
			Batches b = (Batches) model.getObject();
			params.add((++nextParamId).toString(), b.getBatchId().toString());
		}else {
			throw new EduShareRuntimeException(
					"Cant create a home-page url for object of type: "
							+ model.getObject().getClass());
		}

		// Add tabType to parameter
		if (StringUtil.isValid(additional)) {
			params.add((++nextParamId).toString(), additional);
		}

		return params;
	}

	public static PageParameters getParams(IModel<?> model, String str) {
		return getParams1(model, str);
	}

	public static PageParameters getParams(IModel<?> model) {
		return getParams1(model, null);
	}

	public static PageParameters getParams(IModel<?> model, String[] args) {
		PageParameters params = getParams(model);
		Integer numParams = params.size();
		for (String s : args) {
			params.add((numParams++).toString(), s);
		}
		return params;
	}
	
	public static Class<? extends WebPage> getEntityHomePage(IFileUploadable entity){
		
		if(entity instanceof Universities){
			return URLLoader.getPageClass(URLConstants.UNIVERSITY_PROFILE_PATH);
		}else if(entity instanceof Departments){
			return URLLoader.getPageClass(URLConstants.DEPARTMENT_PROFILE_PATH);
		}else if(entity instanceof Courses || entity instanceof CourseInstances){
			return URLLoader.getPageClass(URLConstants.COURSE_PROFILE_PATH);
		}else if(entity instanceof Users){
			return URLLoader.getPageClass(URLConstants.USER_PROFILE_PATH);
		}else if(entity instanceof Groups){
			return URLLoader.getPageClass(URLConstants.GROUPS_NEWS);
		}
		
		throw new EduShareRuntimeException("Entity type might not ne listed allowed entities are univ, dept, courses, users and groups.");
	}
}
